/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.AdlPc;
import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.AdlPcService;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.HomeService;
import me.zhengjie.business.service.ZhsqService;
import me.zhengjie.business.service.dto.AdlPcQueryCriteria;
import me.zhengjie.business.service.dto.HomeQueryCriteria;
import me.zhengjie.business.service.dto.ZhsqQueryCriteria;
import me.zhengjie.pay.domain.PaidQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-05-25
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "首页查询统计")
@RequestMapping("/api/home")
public class HomeController {

    private final HomeService homeService;

    private final GsglService gsglService;

    @GetMapping(value = "/queryGk")
    @ApiOperation("查询首页概况")
    public ResponseEntity<Object> queryGk() {
        boolean isCenter = false;
        Long roleId = 0l;
        UserDetails userDetails = SecurityUtils.getCurrentUser();
        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray rolesArray = new JSONArray(userJson.get("roles"));
        for (int i = 0; i < rolesArray.size(); i++) {
            JSONObject roleJson = rolesArray.getJSONObject(i);
            roleId = roleJson.getLong("id");
            if (roleId == 547) {
                isCenter = true;
                break;
            }
        }

        if (isCenter) {
            return new ResponseEntity<>(homeService.queryGk("", ""),HttpStatus.OK);
        } else {
            Long userid = SecurityUtils.getCurrentUserId();
            Gsgl gsgl = gsglService.queryGsgl(userid);
            return new ResponseEntity<>(homeService.queryGk(gsgl.getBisGsglId(), gsgl.getBisGsglYljgdm()),HttpStatus.OK);
        }

    }

    @GetMapping(value = "/querySettle")
    @ApiOperation("查询首页结算饼图")
    public ResponseEntity<Object> querySettle() {
/*

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
*/

        return new ResponseEntity<>(homeService.querySettle(""),HttpStatus.OK);
    }

    @GetMapping(value = "/queryArea")
    @ApiOperation("查询首页各区域柱状图")
    public ResponseEntity<Object> queryArea() {
/*

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
*/

        return new ResponseEntity<>(homeService.queryArea(""),HttpStatus.OK);
    }

    @ApiOperation("查询首页概况（申请人数）")
    @GetMapping(value = "/queryYsq")
    public ResponseEntity<Object> queryYsq(HomeQueryCriteria criteria, Pageable pageable) {
        boolean isCenter = false;
        Long roleId = 0l;
        UserDetails userDetails = SecurityUtils.getCurrentUser();
        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray rolesArray = new JSONArray(userJson.get("roles"));
        for (int i = 0; i < rolesArray.size(); i++) {
            JSONObject roleJson = rolesArray.getJSONObject(i);
            roleId = roleJson.getLong("id");
            if (roleId == 547) {
                isCenter = true;
                break;
            }
        }

        if (isCenter) {
            return new ResponseEntity<>(homeService.queryYsq(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), "", pageable), HttpStatus.OK);
        } else {
            Long userid = SecurityUtils.getCurrentUserId();
            Gsgl gsgl = gsglService.queryGsgl(userid);
            return new ResponseEntity<>(homeService.queryYsq(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), gsgl.getBisGsglId(), pageable), HttpStatus.OK);
        }

    }

    @ApiOperation("查询首页概况（评估成功）")
    @GetMapping(value = "/queryPgcg")
    public ResponseEntity<Object> queryPgcg(HomeQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(homeService.queryPgcg(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), pageable), HttpStatus.OK);
    }

    @ApiOperation("查询首页概况（评估人次）")
    @GetMapping(value = "/queryPgrc")
    public ResponseEntity<Object> queryPgrc(HomeQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(homeService.queryPgrc(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), criteria.getSftg(), pageable), HttpStatus.OK);
    }

    @ApiOperation("查询首页概况（申请成功）")
    @GetMapping(value = "/querySqcg")
    public ResponseEntity<Object> querySqcg(HomeQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(homeService.querySqcg(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), pageable), HttpStatus.OK);
    }

    @ApiOperation("查询首页概况（已公示）")
    @GetMapping(value = "/queryYgs")
    public ResponseEntity<Object> queryYgs(HomeQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(homeService.queryYgs(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), criteria.getBisGsZt(), pageable), HttpStatus.OK);
    }

    @ApiOperation("查询首页概况（已签约）")
    @GetMapping(value = "/queryYqy")
    public ResponseEntity<Object> queryYqy(HomeQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(homeService.queryYqy(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), criteria.getBisQyZt(), pageable), HttpStatus.OK);
    }

    @ApiOperation("查询首页概况（费用明细）")
    @GetMapping(value = "/queryPgfymx")
    public ResponseEntity<Object> queryPgfymx(HomeQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(homeService.queryPgfymx(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), criteria.getBisQyZt(), pageable), HttpStatus.OK);
    }

    @ApiOperation("查询首页概况（已服务）")
    @GetMapping(value = "/queryYfw")
    public ResponseEntity<Object> queryYfw(HomeQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(homeService.queryYfw(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), pageable), HttpStatus.OK);
    }

    @ApiOperation("查询首页概况（服务人次）")
    @GetMapping(value = "/queryFwrc")
    public ResponseEntity<Object> queryFwrc(HomeQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(homeService.queryFwrc(criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), criteria.getBisZhsqHljgBm(), criteria.getBisOrderZt(), pageable), HttpStatus.OK);
    }
}